<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var merge = function (nums1, m, nums2, n) {
        let p1 = 0,
          p2 = 0;
        const sorted = new Array(m + n).fill(0);
        var cur;
        while (p1 < m || p2 < n) {
          if (p1 === m) {
            cur = nums2[p2++];
          } else if (p2 === n) {
            cur = nums1[p1++];
          } else if (nums1[p1] < nums2[p2]) {
            cur = nums1[p1++];
          } else {
            cur = nums2[p2++];
          }
          sorted[p1 + p2 - 1] = cur;
        }

        return sorted;
      };
      const nums1 = [1, 2, 3, 0, 0, 0],
        m = 3,
        nums2 = [2, 5, 6],
        n = 3;
      const res = merge(nums1, m, nums2, n);
      console.log(res);
    </script>
  </body>
</html>
